<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {

    /**
     * 验证码
     */
    public function verify(){
        $Verify = new \Think\Verify();
        $Verify->useImgBg = false;  //不设置背景图片
        $Verify->imageW = 180;
        $Verify->imageH = 50;
        $Verify->length = 4;
        $Verify->entry();
    }

    /**
     * 注销
     */
    public function logout(){
        session(null);
        $this->success('注销成功',U('Login/index'));
    }

    /**
     * 登录首页和验证登录
     */
    public function index(){
        if(IS_POST){
            //dump(I('post.'));
            //exit;
            //TODO: 优化:这段可以加进UserModel里面。
            //TODO: 优化：登录界面不应该有 header(登录 注册)
            //先验证
//            var_dump($username);
            $username = I('post.username');
            if(is_username($username)){
                $wh['username'] = $username;
            }else if(is_mail($username)){
                $wh['email'] = $username;
            }else if(is_mobile($username)){
                $wh['mobile'] = $username;
            }else{
                $this->error('会员名/手机/电子邮箱格式错误');
            }

            //再验证
            $rules = array(
                array('verify','require','验证码必须！'),
                array('password','is_password','密码必须由6~20字母数字组成',1,'function'),
            );




            $User = M("User"); // 实例化User对象
            if (!$User->validate($rules)->create()) {
                $this->error($User->getError());
            }else{
                //验证通过 可以进行其他数据操作
                if(!check_verify(I('post.verify'),false,'',0)){
                    $this->error('验证码错误');
                }
                $password = I('post.password');
                //因为可以邮箱，手机登录，所以要查用户名
                $one = M('User')->field('id,username,password')->where($wh)->find();
                if($one && $one['password'] == $password ){
                    session('home_uid',$one['id']);
                    session('home_uname',$one['username']);
                    if(I('post.remember') == 1){
                        cookie('remember','1',array('expire'=>3600*7*24,'prefix'=>'home_'));
                        cookie('username',$one['username'],array('expire'=>3600*7*24,'prefix'=>'home_'));
                        cookie('password',$password,array('expire'=>3600*7*24,'prefix'=>'home_'));
                    }else{
                        cookie(null,'home_'); // 清空指定前缀的所有cookie值
                    }

                    if(I('post.return_url')){
                        $this->success('登录成功');
                    }else{
                        $this->success('登录成功',U('Goods/index'));
                    }
                }else{
                    session('home_uid',null);
                    session('home_uname',null);
                    $this->error('用户名或密码错误');
                }
            }
        }else{
            layout(false); //关闭模板布局
            $this->display();
        }
    }

    /**
     * 注册
     */
    public function signup(){
        if(IS_POST){
            $data = I('post.');
            $User = D('User');

            if(I('post.img') == ''){
                $data['img'] = '/jhv/Public/Home/img/user_default.jpg';
            }

            $User->setData($data);

            $res = $User->insertDb();
            if($res){
                $this->success('注册成功',U('index'));
            }else{
                $this->error('注册失败'.$User->getError());
            }

            exit;

        }else{
            $this->display();
        }
    }


    public function check_name($username='',$type=1){
        if(!$username){
            $this->ajaxReturn(0);
        }
        switch ($type) {
            case 1:
                $wh['username'] = $username;
                break;
            case 2:
                $wh['mobile'] = $username;
                break;
            case 3:
                $wh['email'] = $username;
                break;

            default:
                $this->ajaxReturn(0);
                break;
        }

        $one = M('User')->where($wh)->field('id')->find();
        if($one){
            $this->ajaxReturn(2);//被占用
        }else{
            $this->ajaxReturn(1);
        }
    }

    public function upload_img($type=0)
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 2 * 1024 * 1024 ;// 设置附件上传大小 2m
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // $upload->savePath = 'Goods/'; // 设置附件上传（子）目录
        $upload->subName = array('date','Y/m/d');
        // 上传文件
        $info = $upload->uploadOne($_FILES['file']);
        if(!$info) {// 上传错误提示错误信息
            $this->ajaxReturn(array("error"=>$upload->getError()));

        }else{ // 上传成功
            $pic_url = 'Uploads/'.$info['savepath'].$info['savename'];
            $image = new \Think\Image();
            $img = $image->open($pic_url);
            $img->thumb(150, 150,3)->save($pic_url);
            $this->ajaxReturn(array('error'=>'0','pic'=>__ROOT__.'/'.$pic_url,'name'=>$info['savename'],'width'=>'80px'));
        }
    }
}